Development Models > Imperative Programming Model > Imperative Programming Model for OPC Data (Classic and UA) > Obtaining Information (OPC Data) > Reading from OPC Classic Items |
In OPC Data Access, reading data from OPC items is one of the most common tasks. The OPC server generally provides current data for any OPC item in form of a Value, Timestamp and Quality combination (VTQ).
If you want to read the current VTQ from a specific OPC item, call the ReadItem method. You pass in individual arguments for machine name, server class, ItemID, and an optional data type. You will receive back a DAVtq object holding the current value, timestamp, and quality of the OPC item. The ReadItem method returns the current VTQ, regardless of the quality. You may receive an Uncertain or even Bad quality (and no usable data value), and your code needs to deal with such situations accordingly.
// This example shows how to read a single item, and display its value, timestamp and quality. using System; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class ReadItem { public static void Main1() { // Instantiate the client object. var client = new EasyDAClient(); DAVtq vtq; try { vtq = client.ReadItem("", "OPCLabs.KitServer.2", "Simulation.Random"); } catch (OpcException opcException) { Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message); return; } Console.WriteLine("Vtq: {0}", vtq); } } }
# This example shows how to read a single item, and display its value, timestamp and quality. #requires -Version 5.1 using namespace OpcLabs.EasyOpc.DataAccess using namespace OpcLabs.EasyOpc.OperationModel # The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows . Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassicCore.dll" Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassic.dll" Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassicComponents.dll" # Instantiate the client object. $client = New-Object EasyDAClient try { $vtq = [IEasyDAClientExtension]::ReadItem($client, "", "OPCLabs.KitServer.2", "Simulation.Random") } catch [OpcException] { Write-Host "*** Failure: $($PSItem.Exception.GetBaseException().Message)" return } Write-Host "Vtq: $($vtq)"
# This example shows how to read a single item, and display its value, timestamp and quality. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.BaseLib.Navigation import * from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.OperationModel import * # Instantiate the client object. client = EasyDAClient() # Perform the operation. try: vtq = IEasyDAClientExtension.ReadItem(client, '', 'OPCLabs.KitServer.2', 'Simulation.Random') except OpcException as opcException: print('*** Failure: ' + opcException.GetBaseException().Message) exit() # Display results. print('Vtq: ', vtq, sep='')
' This example shows how to read a single item, and display its value, timestamp and quality. Imports OpcLabs.EasyOpc.DataAccess Imports OpcLabs.EasyOpc.OperationModel Namespace DataAccess._EasyDAClient Partial Friend Class ReadItem Public Shared Sub Main1() Dim client = New EasyDAClient() Dim vtq As DAVtq Try vtq = client.ReadItem("", "OPCLabs.KitServer.2", "Simulation.Random") Catch opcException As OpcException Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message) Exit Sub End Try Console.WriteLine("Vtq: {0}", vtq) End Sub End Class End Namespace
// This example shows how to read a single item, and display its value, timestamp and quality. $Client = new COM("OpcLabs.EasyOpc.DataAccess.EasyDAClient"); try { $Vtq = $Client->ReadItem("", "OPCLabs.KitServer.2", "Simulation.Random"); } catch (com_exception $e) { printf("*** Failure: %s\n", $e->getMessage()); Exit(); } printf("Vtq: %s\n", $Vtq);
// This example shows how to read a single item, and display its value, timestamp and quality. var Client = new ActiveXObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient"); var VTQ = Client.ReadItem("", "OPCLabs.KitServer.2", "Simulation.Random"); WScript.Echo("VTQ.ToString(): " + VTQ.ToString());
// This example shows how to read a single item, and display its value, timestamp and quality. class procedure ReadItem.Main; var Client: OpcLabs_EasyOpcClassic_TLB._EasyDAClient; Vtq: _DAVtq; begin // Instantiate the client object Client := CoEasyDAClient.Create; try Vtq := Client.ReadItem('', 'OPCLabs.KitServer.2', 'Simulation.Random'); except on E: EOleException do begin WriteLn(Format('*** Failure: %s', [E.GetBaseException.Message])); Exit; end; end; // Display results WriteLn('Vtq: ', Vtq.ToString); end;
// This example shows how to read a single item, and display its value, timestamp and quality. mle_outputtext.Text = "" // Instantiate the client object OLEObject client client = CREATE OLEObject client.ConnectToNewObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") // Obtain value/timestamp/quality OLEObject vtq TRY vtq = client.ReadItem("", "OPCLabs.KitServer.2", "Demo.Ramp") CATCH (OLERuntimeError oleRuntimeError) mle_outputtext.Text = mle_outputtext.Text + "*** Failure: " + oleRuntimeError.Description + "~r~n" RETURN END TRY // Display results mle_outputtext.Text = mle_outputtext.Text + "Vtq: " + vtq.DisplayString + "~r~n"
Rem This example shows how to read a single item, and display its value, timestamp and quality. Private Sub ReadItem_Main_Command_Click() OutputText = "" ' Instantiate the client object Dim client As New EasyDAClient On Error Resume Next Dim vtq As DAVtq Set vtq = client.ReadItem("", "OPCLabs.KitServer.2", "Simulation.Random") If Err.Number <> 0 Then OutputText = OutputText & "*** Failure: " & Err.Source & ": " & Err.Description & vbCrLf Exit Sub End If On Error GoTo 0 ' Display results OutputText = OutputText & "Vtq: " & vtq & vbCrLf End Sub
Rem This example shows how to read a single item, and display its value, timestamp and quality. Option Explicit Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") On Error Resume Next Dim Vtq: Set Vtq = Client.ReadItem("", "OPCLabs.KitServer.2", "Simulation.Random") If Err.Number <> 0 Then WScript.Echo "*** Failure: " & Err.Source & ": " & Err.Description WScript.Quit End If On Error Goto 0 WScript.Echo "Vtq: " & Vtq
In QuickOPC.NET, you can also pass ServerDescriptor and DAItemDescriptor objects in place of individual arguments to the ReadItem method.
// This example shows how to read a single item using a browse path, and display its value, timestamp and quality. using System; using OpcLabs.EasyOpc; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class ReadItem { public static void BrowsePath() { // Instantiate the client object. var client = new EasyDAClient(); DAVtq vtq; try { vtq = client.ReadItem( new ServerDescriptor("", "OPCLabs.KitServer.2"), new DAItemDescriptor(null, "/Simulation/Random")); } catch (OpcException opcException) { Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message); return; } Console.WriteLine("Vtq: {0}", vtq); } } }
# This example shows how to read a single item using a browse path, and display its value, timestamp and quality. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.BaseLib.Navigation import * from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.OperationModel import * # Instantiate the client object. client = EasyDAClient() # Perform the operation. try: vtq = IEasyDAClientExtension.ReadItem(client, ServerDescriptor('', 'OPCLabs.KitServer.2'), DAItemDescriptor(None, BrowsePath('/Simulation/Random'))) except OpcException as opcException: print('*** Failure: ' + opcException.GetBaseException().Message) exit() # Display results. print('Vtq: ', vtq, sep='')
' This example shows how to read a single item using a browse path, and display its value, timestamp and quality. Imports OpcLabs.EasyOpc Imports OpcLabs.EasyOpc.DataAccess Imports OpcLabs.EasyOpc.OperationModel Namespace DataAccess._EasyDAClient Partial Friend Class ReadItem Public Shared Sub BrowsePath() Dim client = New EasyDAClient() Dim vtq As DAVtq Try vtq = client.ReadItem(New ServerDescriptor("", "OPCLabs.KitServer.2"), New DAItemDescriptor(Nothing, "/Simulation/Random")) Catch opcException As OpcException Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message) Exit Sub End Try Console.WriteLine("Vtq: {0}", vtq) End Sub End Class End Namespace
For reading VTQs of multiple items simultaneously in an efficient manner, call the ReadMultipleItems method (instead of multiple ReadItem calls in a loop). You will receive back an array of DAVtqResult objects.
In QuickOPC.NET, you can pass in a ServerDescriptor object and an array of DAItemDescriptor objects, or an array of DAItemArguments objects, to the ReadMultipleItems method.
// This example shows how to read 4 items at once, and display their values, timestamps and qualities. using System; using System.Diagnostics; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class ReadMultipleItems { public static void Main1() { // Instantiate the client object. var client = new EasyDAClient(); DAVtqResult[] vtqResults = client.ReadMultipleItems("OPCLabs.KitServer.2", new DAItemDescriptor[] { "Simulation.Random", "Trends.Ramp (1 min)", "Trends.Sine (1 min)", "Simulation.Register_I4" }); for (int i = 0; i < vtqResults.Length; i++) { Debug.Assert(vtqResults[i] != null); if (vtqResults[i].Succeeded) Console.WriteLine("vtqResults[{0}].Vtq: {1}", i, vtqResults[i].Vtq); else Console.WriteLine("vtqResults[{0}] *** Failure: {1}", i, vtqResults[i].ErrorMessageBrief); } } } }
# This example shows how to read 4 items at once, and display their values, timestamps and qualities. #requires -Version 5.1 using namespace OpcLabs.EasyOpc.DataAccess # The path below assumes that the current directory is [ProductDir]/Examples-NET/PowerShell/Windows . Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassicCore.dll" Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassic.dll" Add-Type -Path "../../../Components/Opclabs.QuickOpc/net472/OpcLabs.EasyOpcClassicComponents.dll" # Instantiate the client object. $client = New-Object EasyDAClient $vtqResults = [IEasyDAClientExtension]::ReadMultipleItems($client, "OPCLabs.KitServer.2", @( (New-Object DAItemDescriptor("Simulation.Random")), (New-Object DAItemDescriptor("Trends.Ramp (1 min)")), (New-Object DAItemDescriptor("Trends.Sine (1 min)")), (New-Object DAItemDescriptor("Simulation.Register_I4")) )) for ($i = 0; $i -lt $vtqResults.Length; $i++) { $vtqResult = $vtqResults[$i] if ($vtqResult.Succeeded) { Write-Host "vtqResults[$($i)].Vtq: $($vtqResult.Vtq)" } else { Write-Host "vtqResults[$($i)] *** Failure: $($vtqResult.ErrorMessageBrief)" } }
# This example shows how to read 4 items at once, and display their values, timestamps and qualities. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.DataAccess.OperationModel import * from OpcLabs.EasyOpc.OperationModel import * # Instantiate the client object. client = EasyDAClient() # vtqResultArray = client.ReadMultipleItems([ DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Simulation.Random')), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Trends.Ramp (1 min)')), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Trends.Sine (1 min)')), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Simulation.Register_I4')), ]) for i, vtqResult in enumerate(vtqResultArray): assert vtqResult is not None if vtqResult.Succeeded: print('vtqResultArray[', i, '].Vtq: ', vtqResult.Vtq, sep='') else: print('vtqResultArray[', i, '] *** Failure: ', vtqResult.ErrorMessageBrief, sep='')
' This example shows how to read 4 items at once, and display their values, timestamps and qualities. Imports OpcLabs.EasyOpc.DataAccess Imports OpcLabs.EasyOpc.DataAccess.OperationModel Namespace DataAccess._EasyDAClient Partial Friend Class ReadMultipleItems Public Shared Sub Main1() Dim client = New EasyDAClient() Dim vtqResults() As DAVtqResult = client.ReadMultipleItems( "OPCLabs.KitServer.2", New DAItemDescriptor() {"Simulation.Random", "Trends.Ramp (1 min)", "Trends.Sine (1 min)", "Simulation.Register_I4"}) For i = 0 To vtqResults.Length - 1 Debug.Assert(vtqResults(i) IsNot Nothing) If vtqResults(i).Succeeded Then Console.WriteLine("vtqResult[{0}].Vtq: {1}", i, vtqResults(i).Vtq) Else Console.WriteLine("vtqResult[{0}] *** Failure: {1}", i, vtqResults(i).ErrorMessageBrief) End If Next i End Sub End Class End Namespace
// This example shows how to read 4 items at once, and display their values, timestamps and qualities. $ReadItemArguments1 = new COM("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments"); $ReadItemArguments1->ServerDescriptor->ServerClass = "OPCLabs.KitServer.2"; $ReadItemArguments1->ItemDescriptor->ItemID = "Simulation.Random"; $ReadItemArguments2 = new COM("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments"); $ReadItemArguments2->ServerDescriptor->ServerClass = "OPCLabs.KitServer.2"; $ReadItemArguments2->ItemDescriptor->ItemID = "Trends.Ramp (1 min)"; $ReadItemArguments3 = new COM("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments"); $ReadItemArguments3->ServerDescriptor->ServerClass = "OPCLabs.KitServer.2"; $ReadItemArguments3->ItemDescriptor->ItemID = "Trends.Sine (1 min)"; $ReadItemArguments4 = new COM("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments"); $ReadItemArguments4->ServerDescriptor->ServerClass = "OPCLabs.KitServer.2"; $ReadItemArguments4->ItemDescriptor->ItemID = "Simulation.Register_I4"; $arguments[0] = $ReadItemArguments1; $arguments[1] = $ReadItemArguments2; $arguments[2] = $ReadItemArguments3; $arguments[3] = $ReadItemArguments4; $Client = new COM("OpcLabs.EasyOpc.DataAccess.EasyDAClient"); $results = $Client->ReadMultipleItems($arguments); for ($i = 0; $i < count($results); $i++) { $VtqResult = $results[$i]; if ($VtqResult->Succeeded) printf("results[d].Vtq.ToString()s\n", $i, $VtqResult->Vtq->ToString); else printf("results[d]: *** Failures\n", $i, $VtqResult->ErrorMessageBrief); }
// This example shows how to read 4 items at once, and display their values, timestamps and qualities. class procedure ReadMultipleItems.Main; var Arguments: OleVariant; Client: OpcLabs_EasyOpcClassic_TLB._EasyDAClient; I: Cardinal; ReadItemArguments1: _DAReadItemArguments; ReadItemArguments2: _DAReadItemArguments; ReadItemArguments3: _DAReadItemArguments; ReadItemArguments4: _DAReadItemArguments; VtqResult: _DAVtqResult; Results: OleVariant; begin ReadItemArguments1 := CoDAReadItemArguments.Create; ReadItemArguments1.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2'; ReadItemArguments1.ItemDescriptor.ItemID := 'Simulation.Random'; ReadItemArguments2 := CoDAReadItemArguments.Create; ReadItemArguments2.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2'; ReadItemArguments2.ItemDescriptor.ItemID := 'Trends.Ramp (1 min)'; ReadItemArguments3 := CoDAReadItemArguments.Create; ReadItemArguments3.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2'; ReadItemArguments3.ItemDescriptor.ItemID := 'Trends.Sine (1 min)'; ReadItemArguments4 := CoDAReadItemArguments.Create; ReadItemArguments4.ServerDescriptor.ServerClass := 'OPCLabs.KitServer.2'; ReadItemArguments4.ItemDescriptor.ItemID := 'Simulation.Register_I4'; Arguments := VarArrayCreate([0, 3], varVariant); Arguments[0] := ReadItemArguments1; Arguments[1] := ReadItemArguments2; Arguments[2] := ReadItemArguments3; Arguments[3] := ReadItemArguments4; // Instantiate the client object Client := CoEasyDAClient.Create; TVarData(Results).VType := varArray or varVariant; TVarData(Results).VArray := PVarArray( Client.ReadMultipleItems(Arguments)); // Display results for I := VarArrayLowBound(Results, 1) to VarArrayHighBound(Results, 1) do begin VtqResult := IInterface(Results[I]) as _DAVtqResult; if VtqResult.Succeeded then WriteLn('results(', i, ').Vtq.ToString(): ', VtqResult.Vtq.ToString) else WriteLn('results(', i, ') *** Failure: ', VtqResult.ErrorMessageBrief); end; VarClear(Results); VarClear(Arguments); end;
// This example shows how to read 4 items at once, and display their values, timestamps and qualities. mle_outputtext.Text = "" // Instantiate the client object OLEObject client client = CREATE OLEObject client.ConnectToNewObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") // Prepare arguments. OLEObject readItemArguments1 readItemArguments1 = CREATE OLEObject readItemArguments1.ConnectToNewObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") readItemArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" readItemArguments1.ItemDescriptor.ItemID = "Simulation.Random" OLEObject readItemArguments2 readItemArguments2 = CREATE OLEObject readItemArguments2.ConnectToNewObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") readItemArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" readItemArguments2.ItemDescriptor.ItemID = "Trends.Ramp (1 min)" OLEObject readItemArguments3 readItemArguments3 = CREATE OLEObject readItemArguments3.ConnectToNewObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") readItemArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" readItemArguments3.ItemDescriptor.ItemID = "Trends.Sine (1 min)" OLEObject readItemArguments4 readItemArguments4 = CREATE OLEObject readItemArguments4.ConnectToNewObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") readItemArguments4.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" readItemArguments4.ItemDescriptor.ItemID = "Simulation.Register_I4" OLEObject readItemArgumentsList readItemArgumentsList = CREATE OLEObject readItemArgumentsList.ConnectToNewObject("OpcLabs.BaseLib.Collections.ElasticVector") readItemArgumentsList.Add(readItemArguments1) readItemArgumentsList.Add(readItemArguments2) readItemArgumentsList.Add(readItemArguments3) readItemArgumentsList.Add(readItemArguments4) // Obtain values/timestamps/qualities. OLEObject vtqResultList vtqResultList = client.ReadItemList(readItemArgumentsList) // Display results Int i FOR i = 0 TO vtqResultList.Count - 1 OLEObject vtqResult vtqResult = vtqResultList.Item[i] IF vtqResult.Succeeded THEN mle_outputtext.Text = mle_outputtext.Text + "vtqResult[" + String(i) + "].Vtq: " + String(vtqResult.Vtq) + "~r~n" ELSE mle_outputtext.Text = mle_outputtext.Text + "vtqResult[" + String(i) + "] *** Failure: " + vtqResult.ErrorMessageBrief + "~r~n" END IF NEXT
Rem $Header: $ Rem This example shows how to read 4 items at once, and display their values, timestamps and qualities. Dim ReadItemArguments1 As New OLEObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments1.ItemDescriptor.ItemID = "Simulation.Random" Dim ReadItemArguments2 As New OLEObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments2.ItemDescriptor.ItemID = "Trends.Ramp (1 min)" Dim ReadItemArguments3 As New OLEObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments3.ItemDescriptor.ItemID = "Trends.Sine (1 min)" Dim ReadItemArguments4 As New OLEObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments4.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments4.ItemDescriptor.ItemID = "Simulation.Register_I4" Dim argumentsList As New OLEObject("OpcLabs.BaseLib.Collections.ElasticVector") argumentsList.Add(ReadItemArguments1) argumentsList.Add(ReadItemArguments2) argumentsList.Add(ReadItemArguments3) argumentsList.Add(ReadItemArguments4) 'Dim arguments(3) As OLEObject 'arguments(0) = ReadItemArguments1 'arguments(1) = ReadItemArguments2 'arguments(2) = ReadItemArguments3 'arguments(3) = ReadItemArguments4 Dim Client As New OLEObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") Dim resultList As OLEObject resultList = Client.ReadItemList(argumentsList) Dim n As Integer n = resultList.Count 'Dim results() As Variant 'results = Client.ReadMultipleItems(arguments) Dim i As Integer For i = 0 To n - 1 'For i = results.FirstRowIndex To results.LastRowIndex Dim VtqResult As OLEObject VtqResult = resultList.GetAt(i) 'VtqResult = results(i) If VtqResult.Succeeded Then Dim Vtq As OLEObject Vtq = VtqResult.Vtq Print "results(" + CStr(i) + ").Vtq.ToString(): " + Vtq.ToString() Else Print "results(" + CStr(i) + ") *** Failure: " + VtqResult.ErrorMessageBrief End If Next
Rem This example shows how to read 4 items at once, and display their values, timestamps and qualities. Private Sub ReadMultipleItems_Main_Command_Click() OutputText = "" Dim readArguments1 As New DAReadItemArguments readArguments1.serverDescriptor.ServerClass = "OPCLabs.KitServer.2" readArguments1.ItemDescriptor.itemId = "Simulation.Random" Dim readArguments2 As New DAReadItemArguments readArguments2.serverDescriptor.ServerClass = "OPCLabs.KitServer.2" readArguments2.ItemDescriptor.itemId = "Trends.Ramp (1 min)" Dim readArguments3 As New DAReadItemArguments readArguments3.serverDescriptor.ServerClass = "OPCLabs.KitServer.2" readArguments3.ItemDescriptor.itemId = "Trends.Sine (1 min)" Dim readArguments4 As New DAReadItemArguments readArguments4.serverDescriptor.ServerClass = "OPCLabs.KitServer.2" readArguments4.ItemDescriptor.itemId = "Simulation.Register_I4" Dim arguments(3) As Variant Set arguments(0) = readArguments1 Set arguments(1) = readArguments2 Set arguments(2) = readArguments3 Set arguments(3) = readArguments4 ' Instantiate the client object Dim client As New EasyDAClient Dim results() As Variant results = client.ReadMultipleItems(arguments) ' Display results Dim i: For i = LBound(results) To UBound(results) Dim vtqResult As DAVtqResult: Set vtqResult = results(i) If vtqResult.Succeeded Then OutputText = OutputText & "results(" & i & ").Vtq: " & vtqResult.vtq & vbCrLf Else OutputText = OutputText & "results(" & i & ") *** Failure: " & vtqResult.ErrorMessageBrief & vbCrLf End If Next End Sub
Rem This example shows how to read 4 items at once, and display their values, timestamps and qualities. Option Explicit Dim ReadItemArguments1: Set ReadItemArguments1 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments1.ItemDescriptor.ItemID = "Simulation.Random" Dim ReadItemArguments2: Set ReadItemArguments2 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments2.ItemDescriptor.ItemID = "Trends.Ramp (1 min)" Dim ReadItemArguments3: Set ReadItemArguments3 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments3.ItemDescriptor.ItemID = "Trends.Sine (1 min)" Dim ReadItemArguments4: Set ReadItemArguments4 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments4.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments4.ItemDescriptor.ItemID = "Simulation.Register_I4" Dim arguments(3) Set arguments(0) = ReadItemArguments1 Set arguments(1) = ReadItemArguments2 Set arguments(2) = ReadItemArguments3 Set arguments(3) = ReadItemArguments4 Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") Dim results: results = Client.ReadMultipleItems(arguments) Dim i: For i = LBound(results) To UBound(results) Dim VtqResult: Set VtqResult = results(i) If VtqResult.Succeeded Then WScript.Echo "results(" & i & ").Vtq.ToString(): " & VtqResult.Vtq.ToString() Else WScript.Echo "results(" & i & ") *** Failure: " & VtqResult.ErrorMessageBrief End If Next
// This example repeatedly reads a large number of items. using System; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class ReadMultipleItems { private const int RepeatCount = 10; private const int NumberOfItems = 1000; public static void ManyRepeat() { Console.WriteLine("Creating array of arguments..."); var arguments = new DAReadItemArguments[NumberOfItems]; for (int i = 0; i < NumberOfItems; i++) { int copy = (i / 100) + 1; int phase = i % 100; string itemId = FormattableString.Invariant($"Simulation.Incrementing.Copy_{copy}.Phase_{phase}"); Console.WriteLine(itemId); var readItemArguments = new DAReadItemArguments("OPCLabs.KitServer.2", itemId); arguments[i] = readItemArguments; } // Instantiate the client object. var client = new EasyDAClient(); for (int iRepeat = 1; iRepeat <= RepeatCount; iRepeat++) { Console.WriteLine("Reading items..."); DAVtqResult[] vtqResults = client.ReadMultipleItems(arguments); int successCount = 0; foreach (DAVtqResult vtqResult in vtqResults) if (vtqResult.Succeeded) successCount++; Console.WriteLine($"Success count: {successCount}"); } } } }
# This example repeatedly reads a large number of items. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.DataAccess.OperationModel import * from OpcLabs.EasyOpc.OperationModel import * REPEAT_COUNT = 10 NUMBER_OF_ITEMS = 1000 print('Creating array of arguments...') argumentArray = [None]*NUMBER_OF_ITEMS for i in range(NUMBER_OF_ITEMS): copy = (i//100) + 1 phase = i % 100 itemId = 'Simulation.Incrementing.Copy_{}.Phase_{}'.format(copy, phase) print(itemId) # readItemArguments = DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor(itemId)) argumentArray[i] = readItemArguments # Instantiate the client object. client = EasyDAClient() # for iRepeat in range(1, REPEAT_COUNT + 1): print('Reading items...') vtqResultArray = client.ReadMultipleItems(argumentArray) # successCount = 0 for vtqResult in vtqResultArray: if vtqResult.Succeeded: successCount = successCount + 1 print('Success count: ', successCount, sep='') print('Finished.')
Rem This example repeatedly reads a large number of items. Option Explicit Const repeatCount = 10 Const numberOfItems = 1000 WScript.Echo "Creating array of arguments..." Dim arguments(): ReDim arguments(numberOfItems - 1) Dim i: For i = 0 To numberOfItems - 1 Dim copy: copy = Int(i / 100) + 1 Dim phase: phase = i Mod 100 Dim itemId: itemId = "Simulation.Incrementing.Copy_" & copy & ".Phase_" & phase WScript.Echo itemId Dim ReadItemArguments: Set ReadItemArguments = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments.ItemDescriptor.ItemID = itemId Set arguments(i) = ReadItemArguments Next Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") Dim iRepeat: For iRepeat = 1 To repeatCount WScript.Echo "Reading items..." Dim results: results = Client.ReadMultipleItems(arguments) Dim successCount: successCount = 0 For i = LBound(results) To UBound(results) Dim VtqResult: Set VtqResult = results(i) If VtqResult.Succeeded Then successCount = successCount + 1 Next WScript.Echo "Success count: " & successCount Next
// This example shows how to read 4 items at once synchronously, and display their values, timestamps and qualities. using System; using System.Diagnostics; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class ReadMultipleItems { public static void Synchronous() { // Instantiate the client object. var client = new EasyDAClient(); // Specify that only synchronous method is allowed. By default, both synchronous and asynchronous methods are // allowed, and the component picks a suitable method automatically. Disallowing asynchronous method leaves // only the synchronous method available for selection. client.InstanceParameters.Mode.AllowAsynchronousMethod = false; DAVtqResult[] vtqResults = client.ReadMultipleItems("OPCLabs.KitServer.2", new DAItemDescriptor[] { "Simulation.Random", "Trends.Ramp (1 min)", "Trends.Sine (1 min)", "Simulation.Register_I4" }); for (int i = 0; i < vtqResults.Length; i++) { Debug.Assert(vtqResults[i] != null); if (vtqResults[i].Succeeded) Console.WriteLine("vtqResult[{0}].Vtq: {1}", i, vtqResults[i].Vtq); else Console.WriteLine("vtqResult[{0}] *** Failure: {1}", i, vtqResults[i].ErrorMessageBrief); } } // Example output: // //vtqResult[0].Vtq: 0.00125125888851588 { System.Double} @2020-04-10T15:29:20.642; GoodNonspecific(192) //vtqResult[1].Vtq: 0.344052940607071 {System.Double} @2020-04-10T15:29:20.643; GoodNonspecific(192) //vtqResult[2].Vtq: 0.830410616568378 {System.Double} @2020-04-10T15:29:20.643; GoodNonspecific(192) //vtqResult[3].Vtq: 0 {System.Int32} @1601-01-01T00:00:00.000; GoodNonspecific(192) } }
# This example shows how to read 4 items at once synchronously, and display their values, timestamps and qualities. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.DataAccess.OperationModel import * from OpcLabs.EasyOpc.OperationModel import * # Instantiate the client object. client = EasyDAClient() # Specify that only synchronous method is allowed. By default, both synchronous and asynchronous methods are # allowed, and the component picks a suitable method automatically. Disallowing asynchronous method leaves # only the synchronous method available for selection. client.InstanceParameters.Mode.AllowAsynchronousMethod = False # vtqResultArray = client.ReadMultipleItems([ DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Simulation.Random')), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Trends.Ramp (1 min)')), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Trends.Sine (1 min)')), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Simulation.Register_I4')), ]) for i, vtqResult in enumerate(vtqResultArray): assert vtqResult is not None if vtqResult.Succeeded: print('vtqResultArray[', i, '].Vtq: ', vtqResult.Vtq, sep='') else: print('vtqResultArray[', i, '] *** Failure: ', vtqResult.ErrorMessageBrief, sep='')
Rem This example shows how to read 4 items at once synchronously, and display their values, timestamps and qualities. Option Explicit Dim ReadItemArguments1: Set ReadItemArguments1 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments1.ItemDescriptor.ItemID = "Simulation.Random" Dim ReadItemArguments2: Set ReadItemArguments2 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments2.ItemDescriptor.ItemID = "Trends.Ramp (1 min)" Dim ReadItemArguments3: Set ReadItemArguments3 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments3.ItemDescriptor.ItemID = "Trends.Sine (1 min)" Dim ReadItemArguments4: Set ReadItemArguments4 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments4.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments4.ItemDescriptor.ItemID = "Simulation.Register_I4" Dim arguments(3) Set arguments(0) = ReadItemArguments1 Set arguments(1) = ReadItemArguments2 Set arguments(2) = ReadItemArguments3 Set arguments(3) = ReadItemArguments4 Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") ' Specify that only synchronous method is allowed. By default, both synchronous and asynchronous methods are allowed, and ' the component picks a suitable method automatically. Disallowing asynchronous method leaves only the synchronous method ' available for selection. Client.InstanceParameters.Mode.AllowAsynchronousMethod = False Dim results: results = Client.ReadMultipleItems(arguments) Dim i: For i = LBound(results) To UBound(results) Dim VtqResult: Set VtqResult = results(i) If VtqResult.Succeeded Then WScript.Echo "results(" & i & ").Vtq.ToString(): " & VtqResult.Vtq.ToString() Else WScript.Echo "results(" & i & ") *** Failure: " & VtqResult.ErrorMessageBrief End If Next
Rem This example shows how to read 2 items (first valid, second invalid), test for success of each read and display either Rem the DAVtq or the Exception. Option Explicit Dim ReadItemArguments1: Set ReadItemArguments1 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments1.ItemDescriptor.ItemID = "Simulation.Random" Dim ReadItemArguments2: Set ReadItemArguments2 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments2.ItemDescriptor.ItemID = "UnknownItem" Dim arguments(1) Set arguments(0) = ReadItemArguments1 Set arguments(1) = ReadItemArguments2 Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") Dim results: results = Client.ReadMultipleItems(arguments) Dim i: For i = LBound(results) To UBound(results) Dim VtqResult: Set VtqResult = results(i) If VtqResult.Succeeded Then WScript.Echo "results(" & i & ").Vtq.ToString(): " & VtqResult.Vtq.ToString() Else WScript.Echo "results(" & i & ") *** Failure: " & VtqResult.ErrorMessageBrief End If Next
# This example shows how to read 2 items (first valid, second invalid), test for success of each read and display either # the DAVtq or the Exception. # The Python for Windows (pywin32) extensions package is needed. Install it using "pip install pypiwin32". # CAUTION: We now recommend using Python.NET package instead. Full set of examples with Python.NET is available! import win32com.client # Instantiate the client object client = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.EasyDAClient') # readItemArguments1 = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments') readItemArguments1.ServerDescriptor.ServerClass = 'OPCLabs.KitServer.2' readItemArguments1.ItemDescriptor.ItemID = 'Simulation.Random' readItemArguments2 = win32com.client.Dispatch('OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments') readItemArguments2.ServerDescriptor.ServerClass = 'OPCLabs.KitServer.2' readItemArguments2.ItemDescriptor.ItemID = 'UnknownItem' arguments = [ readItemArguments1, readItemArguments2, ] results = client.ReadMultipleItems(arguments) for i, vtqResult in enumerate(results): if vtqResult.Succeeded: print('results(' + str(i) + '): ' + str(vtqResult.Vtq)) else: print('results(' + str(i) + ') *** Failure: ' + vtqResult.ErrorMessageBrief)
Rem This example attempts to read 3 items (first valid, second invalid, third valid again), and display their values, Rem timestamps and qualities. Without testing for a success, a run-time error occurs when accessing the Vtq property Rem of a failed result. Option Explicit Dim ReadItemArguments1: Set ReadItemArguments1 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments1.ItemDescriptor.ItemID = "Simulation.Random" Dim ReadItemArguments2: Set ReadItemArguments2 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments2.ItemDescriptor.ItemID = "UnknownItem" Dim ReadItemArguments3: Set ReadItemArguments3 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments3.ItemDescriptor.ItemID = "Trends.Ramp (1 min)" Dim arguments(2) Set arguments(0) = ReadItemArguments1 Set arguments(1) = ReadItemArguments2 Set arguments(2) = ReadItemArguments3 Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") Dim results: results = Client.ReadMultipleItems(arguments) Dim i: For i = LBound(results) To UBound(results) ' This is an anti-example - you should not access the Vtq property without testing the success first! WScript.Echo "results(" & i & ").Vtq.ToString(): " & results(i).Vtq.ToString() Next
In QuickOPC.NET, you can use the optional argument of type DAReadParameters, and then set the ValueAge property in it to control how “old” may be the values you receive by reading from OPC items. With the DataSource property, you can also specify that you want values from the device, or from cache. You can also use the pre-defined DAReadParameters.CacheSource and DAReadParameters.DeviceSource constants to specify the read parameters. There are also overloads of ReadXXXX methods that accept an integer valueAge argument.
In .NET languages, there is an implicit conversion operator from Int32 to DAReadParameters, which means that you can simply use the number of milliseconds that represents the value age in place of any argument that expects the read parameters (the DAReadParameters object). There is also a FromInt32 Method which has the same functionality as this conversion operator.
In addition, there is also an implicit conversion from the DADataSource Enumeration to DAReadParameters. You can therefore use the enumeration members such as DADataSource.Cache or DADataSource.Device in place of the read parameters. The FromDADataSource Method has the same functionality as this conversion operator.
The default setting of read parameters, which specifies a non-zero value age, may sometimes "bite" you, if you expect to receive an up-to-date value from the data source. Furthermore, you may even receive a "bad quality" indication with some first reads, because the (OPC server) cache is not yet filled with valid data. Make sure to specify the value age that is appropriate for particular read operation in your application, if the default behavior is not what you want.
Reading from the device (or using very short value age) is, however, an ineffective practice that is discouraged, and should only be used when absolutely necessary.
Be aware that it is physically impossible for any system to always obtain fully up-to-date values all the time.
// This example shows how to read a single item from the device, and display its value, timestamp and quality. using System; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class ReadItem { public static void DeviceSource() { // Instantiate the client object. var client = new EasyDAClient(); Console.WriteLine("Reading item..."); DAVtq vtq; try { // DADataSource enumeration: // Selects the data source for OPC reads (from device, from OPC cache, or dynamically determined). // The data source (memory, OPC cache or OPC device) selection is based on the desired value age and // current status of data received from the server. vtq = client.ReadItem("OPCLabs.KitServer.2", "Simulation.Random", DADataSource.Device); } catch (OpcException opcException) { Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message); return; } Console.WriteLine("Vtq: {0}", vtq); } } }
# This example shows how to read a single item from the device, and display its value, timestamp and quality. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.OperationModel import * # Instantiate the client object. client = EasyDAClient() print('Reading item...') try: # DADataSource enumeration: # Selects the data source for OPC reads (from device, from OPC cache, or dynamically determined). # The data source (memory, OPC cache or OPC device) selection is based on the desired value age and # current status of data received from the server. vtq = IEasyDAClientExtension.ReadItem(client, ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Simulation.Random'), DAReadParameters(DADataSource.Device)) except OpcException as opcException: print('*** Failure: ' + opcException.GetBaseException().Message) exit() # Display results. print('Vtq: ', vtq, sep='')
// This example shows how to read 4 items from the device, and display their values, timestamps and qualities. using System; using System.Diagnostics; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.DataAccess.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class ReadMultipleItems { public static void DeviceSource() { // Instantiate the client object. var client = new EasyDAClient(); // DADataSource enumeration: // Selects the data source for OPC reads (from device, from OPC cache, or dynamically determined). // The data source (memory, OPC cache or OPC device) selection will be based on the desired value age and // current status of data received from the server. DAVtqResult[] vtqResults = client.ReadMultipleItems( new [] { new DAReadItemArguments("OPCLabs.KitServer.2", "Simulation.Random", DADataSource.Device), new DAReadItemArguments("OPCLabs.KitServer.2", "Trends.Ramp (1 min)", DADataSource.Device), new DAReadItemArguments("OPCLabs.KitServer.2", "Trends.Sine (1 min)", DADataSource.Device), new DAReadItemArguments("OPCLabs.KitServer.2", "Simulation.Register_I4", DADataSource.Device) }); for (int i = 0; i < vtqResults.Length; i++) { Debug.Assert(vtqResults[i] != null); if (vtqResults[i].Succeeded) Console.WriteLine("vtqResult[{0}].Vtq: {1}", i, vtqResults[i].Vtq); else Console.WriteLine("vtqResult[{0}] *** Failure: {1}", i, vtqResults[i].ErrorMessageBrief); } } // Example output: // //vtqResult[0].Vtq: 0.00125125888851588 { System.Double} @2020-04-10T12:44:16.250; GoodNonspecific(192) //vtqResult[1].Vtq: 0.270812898874283 {System.Double} @2020-04-10T12:44:16.248; GoodNonspecific(192) //vtqResult[2].Vtq: 0.991434340167834 {System.Double} @2020-04-10T12:44:16.250; GoodNonspecific(192) //vtqResult[3].Vtq: 0 {System.Int32} @1601-01-01T00:00:00.000; GoodNonspecific(192) } }
# This example shows how to read 4 items from the device, and display their values, timestamps and qualities. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.DataAccess.OperationModel import * from OpcLabs.EasyOpc.OperationModel import * # Instantiate the client object. client = EasyDAClient() # DADataSource enumeration: # Selects the data source for OPC reads (from device, from OPC cache, or dynamically determined). # The data source (memory, OPC cache or OPC device) selection is based on the desired value age and # current status of data received from the server. # vtqResultArray = client.ReadMultipleItems([ DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Simulation.Random'), DAReadParameters(DADataSource.Device)), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Trends.Ramp (1 min)'), DAReadParameters(DADataSource.Device)), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Trends.Sine (1 min)'), DAReadParameters(DADataSource.Device)), DAReadItemArguments(ServerDescriptor('OPCLabs.KitServer.2'), DAItemDescriptor('Simulation.Register_I4'), DAReadParameters(DADataSource.Device)), ]) for i, vtqResult in enumerate(vtqResultArray): assert vtqResult is not None if vtqResult.Succeeded: print('vtqResultArray[', i, '].Vtq: ', vtqResult.Vtq, sep='') else: print('vtqResultArray[', i, '] *** Failure: ', vtqResult.ErrorMessageBrief, sep='')
Rem This example shows how to read 4 items from the device, and display their values, timestamps and qualities. Option Explicit ' Selects the data source for OPC reads (from device, from OPC cache, or dynamically determined). ' The data source (memory, OPC cache or OPC device) selection will be based on the desired value age and current status of ' data received from the server. Const DADataSource_ByValueAge = 0 ' OPC reads will be fulfilled from the cache in the OPC server. Const DADataSource_Cache = 1 ' OPC reads will be fulfilled from the device by the OPC server. Const DADataSource_Device = 2 Dim ReadItemArguments1: Set ReadItemArguments1 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments1.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments1.ItemDescriptor.ItemID = "Simulation.Random" ReadItemArguments1.ReadParameters.DataSource = DADataSource_Device ' read will be from device Dim ReadItemArguments2: Set ReadItemArguments2 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments2.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments2.ItemDescriptor.ItemID = "Trends.Ramp (1 min)" ReadItemArguments2.ReadParameters.DataSource = DADataSource_Device ' read will be from device Dim ReadItemArguments3: Set ReadItemArguments3 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments3.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments3.ItemDescriptor.ItemID = "Trends.Sine (1 min)" ReadItemArguments3.ReadParameters.DataSource = DADataSource_Device ' read will be from device Dim ReadItemArguments4: Set ReadItemArguments4 = CreateObject("OpcLabs.EasyOpc.DataAccess.OperationModel.DAReadItemArguments") ReadItemArguments4.ServerDescriptor.ServerClass = "OPCLabs.KitServer.2" ReadItemArguments4.ItemDescriptor.ItemID = "Simulation.Register_I4" ReadItemArguments4.ReadParameters.DataSource = DADataSource_Device ' read will be from device Dim arguments(3) Set arguments(0) = ReadItemArguments1 Set arguments(1) = ReadItemArguments2 Set arguments(2) = ReadItemArguments3 Set arguments(3) = ReadItemArguments4 Dim Client: Set Client = CreateObject("OpcLabs.EasyOpc.DataAccess.EasyDAClient") Dim results: results = Client.ReadMultipleItems(arguments) Dim i: For i = LBound(results) To UBound(results) Dim VtqResult: Set VtqResult = results(i) If VtqResult.Succeeded Then WScript.Echo "results(" & i & ").Vtq.ToString(): " & VtqResult.Vtq.ToString() Else WScript.Echo "results(" & i & ") *** Failure: " & VtqResult.ErrorMessageBrief End If Next
// This example shows how to read an item synchronously, and display its value, timestamp and quality. using System; using OpcLabs.EasyOpc.DataAccess; using OpcLabs.EasyOpc.OperationModel; namespace DocExamples.DataAccess._EasyDAClient { partial class ReadItem { public static void Synchronous() { // Instantiate the client object. var client = new EasyDAClient(); // Specify that only synchronous method is allowed. By default, both synchronous and asynchronous methods are // allowed, and the component picks a suitable method automatically. Disallowing asynchronous method leaves // only the synchronous method available for selection. client.InstanceParameters.Mode.AllowAsynchronousMethod = false; DAVtq vtq; try { vtq = client.ReadItem("", "OPCLabs.KitServer.2", "Simulation.Random"); } catch (OpcException opcException) { Console.WriteLine("*** Failure: {0}", opcException.GetBaseException().Message); return; } Console.WriteLine("Vtq: {0}", vtq); } } }
# This example shows how to read an item synchronously, and display its value, timestamp and quality. # The QuickOPC package is needed. Install it using "pip install opclabs_quickopc". import opclabs_quickopc # Import .NET namespaces. from OpcLabs.EasyOpc import * from OpcLabs.EasyOpc.DataAccess import * from OpcLabs.EasyOpc.OperationModel import * # Instantiate the client object. client = EasyDAClient() # Specify that only synchronous method is allowed. By default, both synchronous and asynchronous methods are # allowed, and the component picks a suitable method automatically. Disallowing asynchronous method leaves # only the synchronous method available for selection. client.InstanceParameters.Mode.AllowAsynchronousMethod = False # Perform the operation. try: vtq = IEasyDAClientExtension.ReadItem(client, '', 'OPCLabs.KitServer.2', 'Simulation.Random') except OpcException as opcException: print('*** Failure: ' + opcException.GetBaseException().Message) exit() # Display results. print('Vtq: ', vtq, sep='')
Copyright © 2004-2023 CODE Consulting and Development, s.r.o., Plzen. All rights reserved. Web page: www.opclabs.com
Send Documentation Feedback. Resources: Knowledge Base. Technical support: Online Forums, FAQ.